﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace atmsystem_ss
{
    public partial class frmLogin_Admin : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dt = new DataTable("Admin");
        private SqlDataAdapter da = new SqlDataAdapter();
        private void connect() 
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try{
            con = new SqlConnection(cn);
            con.Open();//mo ket noi

            }catch(Exception ex){
            MessageBox.Show("Khong ket noi duoc co so du lieu","Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
        private void disconnect() {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }
        public frmLogin_Admin()
        {
            InitializeComponent();
        }

        private void frmLogin_Admin_Load(object sender, EventArgs e)
        {
            connect();
            txtID_Admin.Text = "Admin";
            this.AcceptButton = btnLogin;
            
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * From tblAdmin Where (fldID_Admin = @fldID_Admin) And (fldPassword_Admin = @fldPassword_Admin)";
            command.Parameters.Add("@fldID_Admin", SqlDbType.NVarChar,50).Value = txtID_Admin.Text;
            command.Parameters.Add("@fldPassword_Admin", SqlDbType.NVarChar, 50).Value = txtPassword_Admin.Text;
            da.SelectCommand = command;
            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                frmAdmin _frmAdmin = new frmAdmin();
                _frmAdmin.Show();
                Hide();
                
            }
            else
            {
               if(MessageBox.Show("Dang nhap that bai, ban co muon dang nhap lai khong", "Login", MessageBoxButtons.YesNo)== DialogResult.Yes)
                {
                txtID_Admin.Focus();
                }
                else
                {
                Close();

                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            frm_Login_Account_Customer _frm_Login_Account_Customer = new frm_Login_Account_Customer();
            _frm_Login_Account_Customer.Show();
            Close();
            disconnect();
        }
    }
}
